package com.yonyou.pmclouds.billview.rmiimpl.buzi;

import com.yonyou.pmclouds.alter.entity.AlterVO;
import com.yonyou.pmclouds.alter.rmiitf.AlterQuery;
import com.yonyou.pmclouds.basecom.consts.BillTypeConst;
import com.yonyou.pmclouds.basecom.entity.PageObject;
import com.yonyou.pmclouds.basecom.entity.SuperVO;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.util.CommonFieldConst;
import com.yonyou.pmclouds.billview.entity.BillViewQueryVO;
import com.yonyou.pmclouds.billview.rmitf.IBillViewBuziService;
import com.yonyou.pmclouds.billview.util.BillViewUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.DecimalFormat;

@Component
public class AlterBuziServiceImpl implements IBillViewBuziService {
    @Autowired
    private AlterQuery alterQuery;
    @Override
    public String getBillType() {
        return BillTypeConst.ALTER_BILLTYPE;
    }

    @Override
    public PageObject<Object> queryBillList(BillViewQueryVO billViewQueryVO) throws BusinessException {

        PageObject<Object> pageResult = alterQuery.queryByCondition(billViewQueryVO.getPkProject(),billViewQueryVO.getCreators(),billViewQueryVO.getStartTime(),
                billViewQueryVO.getEndTime(),billViewQueryVO.getBillStatus(),billViewQueryVO.getPageStart(),billViewQueryVO.getPageSize());
        Object[] objects = pageResult.getObjects();

        DecimalFormat df = new DecimalFormat( "0.00");
        AlterVO tempAlterVO = new AlterVO();
        for(Object obj : objects){
            tempAlterVO = ((AlterVO)obj);
            if(tempAlterVO.getEstimateMny() == null){
                tempAlterVO.setStrEstimateMny("");
            }else{
                tempAlterVO.setStrEstimateMny(df.format(tempAlterVO.getEstimateMny().doubleValue()));
            }
        }

        BillViewUtil.setBillStatus(objects);

        pageResult.setObjects(objects);
        return pageResult;
    }

    @Override
    public SuperVO queryBillDetail(String pkBill) throws BusinessException {

        AlterVO alterVO = alterQuery.getById(pkBill);

        if(alterVO != null && alterVO.getEstimateMny() != null){
            DecimalFormat df = new DecimalFormat( "0.00");
            alterVO.setStrEstimateMny(df.format(alterVO.getEstimateMny()));
        }

        BillViewUtil.setBillStatus(new Object[]{alterVO});
        BillViewUtil.setFormatDate(new AlterVO[]{alterVO},new String[]{CommonFieldConst.CREATIONTIME,"applyDate"},new String[]{"strCreationtime","strApplyDate"});

        return alterVO;
    }
}
